package com.hw.backapi;


import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;

/**
 * @author lixiaopeng
 * @date 2022-07-17 10:10
 */
public class MybatisPlusGenerator {
    public static void main(String[] args) throws Exception {
        FastAutoGenerator fastAutoGenerator = FastAutoGenerator.create(
                "jdbc:postgresql://43.138.27.38:5432/hw2",
                "postgres",
                "phpts"
        );


        DataSourceConfig dataSourceConfig = new DataSourceConfig
                .Builder(
                "jdbc:postgresql://43.138.27.38:5432/hw2",
                "postgres",
                "phpts")
                .schema("public")
                .build();

        AutoGenerator autoGenerator = new AutoGenerator(dataSourceConfig);

        //
        String output = System.getProperty("user.dir");

        GlobalConfig globalConfig = new GlobalConfig.Builder()
                .disableOpenDir()
                .outputDir(output + "\\model\\src\\main\\java\\")
                .build();
        autoGenerator.global(globalConfig);

        PackageConfig packageConfig = new PackageConfig.Builder()
                .parent("com.hw.model") // 设置父包名
                .mapper("mapper") // 设置 mapper 包名
                .service("service")
                .serviceImpl("service.impl")
                .xml("mapperxml")
                .pathInfo(Collections.singletonMap(OutputFile.xml, output + "\\model\\src\\main\\resources\\mapper"))
                .build();
        autoGenerator.packageInfo(packageConfig);

        StrategyConfig strategyConfig = new StrategyConfig.Builder()
                .addInclude(
                        "member"
                ) // 设置需要生成的表名
                .addTablePrefix("t_") // 设置过滤表前缀
                .entityBuilder().enableLombok()
                .convertFileName(entityName -> entityName + "PO")
                .build();
        autoGenerator.strategy(strategyConfig);


        InjectionConfig injectionConfig = new InjectionConfig.Builder().fileOverride().build();// 覆盖生成的文件
        autoGenerator.injection(injectionConfig);

        TemplateConfig templateConfig = new TemplateConfig.Builder().disable(TemplateType.CONTROLLER).build();
        autoGenerator.template(templateConfig);

        autoGenerator.execute(new FreemarkerTemplateEngine());
    }

}
